<?php
set_time_limit(120);
require_once("mysql.php");
//require_once("functions.php");
/*ORDER REMINDER*/
$day1=86400;
date_default_timezone_set('Europe/Moscow');
$time=time()+$day1;
$now_time=time();
$query = "SELECT * FROM `orders` WHERE `oplachenodo`<='$time' AND `status`>'1'";
$sql = mysql_query($query) or die(mysql_error());
while ($tablerows = mysql_fetch_array($sql))
{
  $cost=$tablerows['cost'];
  $orderid=$tablerows['id'];
  $userid=$tablerows['userid'];
  $due=$tablerows['oplachenodo'];
  $query1 = "SELECT * FROM `users` WHERE `id`='$userid' LIMIT 1";
  $sql1 = mysql_query($query1) or die(mysql_error());
  $user=mysql_fetch_array($sql1);
  $enable_sms=$user['sms'];
  $login=$user['login'];
  if($enable_sms==1)
  {
    $sms_number=$user['contacts'];
    $sms_number=str_replace('+','',$sms_number);
    if(!preg_match("/^[0-9]{10,13}+$/", $sms_number))
    {
      echo ("Телефон задан в неверном формате");
    }
    else
    {
      //send sms
      $response= send("api.smsfeedback.ru", 80, "falcon007", "slava13",
            $sms_number, "Вам необходимо пополнить баланс для оплаты хостинга на www.radioheart.ru", "RadioHeart");
      echo $response;
      //log sms send
      $time=time();
      $sms_number=mysql_escape_string($sms_number);
      $login=mysql_escape_string($login);
      $response=mysql_escape_string($response);
      $query="INSERT INTO `sms_log` (`id`, `date`, `number`, `user`, `response`) VALUES (NULL, '{$time}', '{$sms_number}', '{$login}', '{$response}');";
      $sql1 = mysql_query($query) or die(mysql_error());
    }
  }
}
function send($host, $port, $login, $password, $phone, $text, $sender = false, $wapurl = false )
{
    $fp = fsockopen($host, $port, $errno, $errstr);
    if (!$fp) {
        return "errno: $errno \nerrstr: $errstr\n";
    }
    fwrite($fp, "GET /messages/v2/send/" .
        "?phone=" . rawurlencode($phone) .
        "&text=" . rawurlencode($text) .
        ($sender ? "&sender=" . rawurlencode($sender) : "") .
        ($wapurl ? "&wapurl=" . rawurlencode($wapurl) : "") .
        "  HTTP/1.0\n");
    fwrite($fp, "Host: " . $host . "\r\n");
    if ($login != "") {
        fwrite($fp, "Authorization: Basic " .
            base64_encode($login. ":" . $password) . "\n");
    }
    fwrite($fp, "\n");
    $response = "";
    while(!feof($fp)) {
        $response .= fread($fp, 1);
    }
    fclose($fp);
    list($other, $responseBody) = explode("\r\n\r\n", $response, 2);
    return $responseBody;
}

?>